package com.ht.medicaltreatment.viewmodel

import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnHttpListener
import com.hjq.http.listener.OnUpdateListener
import com.ht.medicaltreatment.api.PrepareConsumableAPI
import com.ht.medicaltreatment.bean.ConsumableRecordBean
import com.ht.medicaltreatment.bean.ConsumableRecordData
import com.ht.medicaltreatment.bean.PrepareConsumableFileBean
import com.ht.medicaltreatment.http.api.PostConsumableFileAPI
import java.io.File

class PrepareConsumableViewModel :BaseViewModel() {
    fun postFile(path:String,projectId: Int,id:Int,sheetNumber:Int,onResult: (PrepareConsumableFileBean) -> Unit){
        val file = File(path)
        EasyHttp.post(this)
            .api(PostConsumableFileAPI(
                excelfile = file,
                projectId = projectId,
                projectConsumableRecordId = id,
                sheetNumber = sheetNumber))
            .request(object : OnUpdateListener<PrepareConsumableFileBean> {
                override fun onUpdateProgressChange(progress: Int) {

                }

                override fun onUpdateFail(e: java.lang.Exception?) {
                    dismissProgress("导入失败")
                }

                override fun onUpdateSuccess(result: PrepareConsumableFileBean) {
                    onResult(result)
                }
            })
    }

    fun requestList(projectId:Int,onSuccess:(MutableList<ConsumableRecordData>)->Unit){
        EasyHttp.post(this)
            .api(PrepareConsumableAPI(projectId))
            .request(object : OnHttpListener<ConsumableRecordBean> {
                override fun onHttpSuccess(result: ConsumableRecordBean) {
                    onSuccess(result.data)
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message?:"请求错误")
                }
            })
    }
}